#!/bin/bash

echo "ALTER USER 'root'@'localhost' IDENTIFIED BY '$MYSQL_PASSWORD';" > /tmp/mysql-init.sql;
echo "grant all privileges on *.* to root@'127.0.0.1' identified by '$MYSQL_PASSWORD';" >>  /tmp/mysql-init.sql;

if [ ! "$(ls -A /var/lib/mysql)" ]; then
    mysqld --explicit_defaults_for_timestamp --initialize --datadir=/var/lib/mysql --init-file=/tmp/mysql-init.sql --user=mysql
    chown -R mysql:mysql /var/lib/mysql
    echo "mysqlå init success"
fi

mysqldomain=`cat /etc/hosts | grep $MYSQL_DOMAIN`
if [ -z "$mysqldomain" ];then
   echo "$MYSQL_IP $MYSQL_DOMAIN" >> /etc/hosts
fi

chown -R mysql:mysql /var/lib/mysql
